package com.carsa.blackberry.ui.screen.entregasfallidas;

import java.util.Enumeration;
import java.util.Hashtable;

import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.component.LabelField;

import com.carsa.blackberry.CarsaApp;
import com.carsa.blackberry.MainKeys;
import com.carsa.blackberry.domain.dto.ArticuloDTO;
import com.carsa.blackberry.domain.dto.EntregaDTO;
import com.carsa.blackberry.domain.dto.ReasonDTO;
import com.carsa.blackberry.ui.UIKeys;
import com.carsa.blackberry.ui.screen.common.BasicScreen;
import com.carsa.blackberry.util.ui.UIUtil;
import com.carsa.blackberry.util.ui.widget.FormField;

/**
 * @author Nahuel Barrios.
 * @since 20/07/2010.
 * @version 1.0
 */
public class DetalleEntregaFallidaScreen extends BasicScreen {

    public DetalleEntregaFallidaScreen(Hashtable parameters) {
	super(UIKeys.TITLE_ENTREGA_SELECCIONADA);
	super.customizeScreen();

	EntregaDTO elArticulo = (EntregaDTO) parameters.get(MainKeys.PARAMETER_CURRENT_ENTREGA);

	FormField headerForm = new FormField(FormField.STYLE_READ_ONLY, new int[] { 100, 220 });
	headerForm.addField(UIKeys.FIELD_ENTREGA, elArticulo.getVbeln(), Field.FOCUSABLE);
	headerForm.addField(UIKeys.FIELD_CLIENTE, (String) parameters.get(MainKeys.PARAMETER_CLIENT_NAME),
	        Field.NON_FOCUSABLE);
	headerForm.addField(UIKeys.FIELD_ADDRESS, (String) parameters.get(MainKeys.PARAMETER_CLIENT_DIREC),
	        Field.NON_FOCUSABLE);

	_mainManager.add(headerForm);

	_mainManager.add(UIUtil.getEmptyLine());
	LabelField titleArticulos = new LabelField(UIKeys.FIELD_ARTICULOS);
	titleArticulos.setFont(this.getFont().getFontFamily().getFont(Font.BOLD | Font.ITALIC, 18));
	_mainManager.add(titleArticulos);

	Enumeration articles = elArticulo.getArticles().elements();
	while (articles.hasMoreElements()) {
	    ArticuloDTO anArticle = (ArticuloDTO) articles.nextElement();

	    if (anArticle.wasCalifiedAsFail()) {
		FormField form = new FormField(FormField.STYLE_READ_ONLY, new int[] { 100, 220 });

		form.addField(UIKeys.FIELD_DESCRIPTION, anArticle.getArktx(), Field.FOCUSABLE);

		Hashtable reasons = (Hashtable) CarsaApp.getMemory().get(MainKeys.PARAMETER_REASONS_AND_SUBREASONS);
		ReasonDTO theReason = (ReasonDTO) reasons.get(anArticle.getGrund());
		String reasonDescript = "";
		String secondReasonDescript = "";
		if (theReason != null) {
		    reasonDescript = theReason.getReasonDescript();
		    secondReasonDescript = theReason.getSubreason(anArticle.getSubmo()).getDescript();
		}

		form.addField(UIKeys.FIELD_REASON, reasonDescript, Field.NON_FOCUSABLE);
		form.addField(UIKeys.FIELD_SECOND_REASON, secondReasonDescript, Field.NON_FOCUSABLE);

		_mainManager.add(UIUtil.getEmptyLineForResize(2));
		_mainManager.add(form);

	    }
	}

	_mainManager.add(UIUtil.getEmptyLine());
	super.addBackButton(_mainManager);
	this.add(_mainManager);
    }

}
